package javagamecontroller.connection;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.Serializable;
import util.input.android.events.AndroidControllerConfigMessage;
import util.input.android.events.AndroidServerMessage;
import android.bluetooth.BluetoothSocket;
import android.util.Log;


/**
 * This thread runs during a connection with a remote device.
 * It handles all incoming and outgoing transmissions.
 */
public class MessengerThread extends Thread {

    private BluetoothMessenger myMessenger;
    private final BluetoothSocket mySocket;
    private final InputStream myInputStream;
    private final OutputStream myOutputStream;

    public MessengerThread (BluetoothSocket socket, BluetoothMessenger messenger) {
        // Log.d(TAG, "create ConnectedThread");
        mySocket = socket;
        myMessenger = messenger;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // Get the BluetoothSocket input and output streams
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        }
        catch (IOException e) {
            Log.e("messenger thread", "temp sockets not created", e);
        }
        Log.e("messenger thread", "temp sockets  created");
        myInputStream = tmpIn;
        myOutputStream = tmpOut;
    }

    public void run () {

        while (true) {
            try {
                ObjectInputStream objectStream = new ObjectInputStream(myInputStream);
                Serializable androidEvent = (Serializable) objectStream.readObject();
                handleObject(androidEvent);
            }
            catch (IOException e) {
                Log.e("messengerThread", "disconnected", e);
                myMessenger.connectionLost();
                break;
            }
            catch (ClassNotFoundException e) {
                Log.e("messengerThread", e.toString());
            }
        }
    }

    private void handleObject (Serializable androidMessage) {
        if (androidMessage instanceof AndroidServerMessage) {
            AndroidServerMessage msg = (AndroidServerMessage) androidMessage;
            if (msg.getMessageBank().size() > 0) {
                myMessenger.setControllerDescription(msg.getMessageBank());
            }
            if (msg.getControllerConfigSettings() != null) {
                myMessenger.setControllerConfig(msg.getControllerConfigSettings());
            }
            if (msg.getAndroidVibration() != null) {
                myMessenger.performVibration(msg.getAndroidVibration());
            }
        }
    }

    /**
     * Write to the connected OutStream.
     * 
     * @param buffer The bytes to write
     */
    public void write (byte[] buffer) {
        try {
            myOutputStream.write(buffer);
        }
        catch (IOException e) {
            Log.e("MessengerThread", "Exception during write", e);
        }
    }
   
    /**
     * Cancel the connection
     */
    public void cancel () {
        try {
            mySocket.close();
        }
        catch (IOException e) {
        }
    }

}
